﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginecup.sprite.changeSourceSize;
using Imaginecup.sprite;
using imaginecup.util;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.util;
using Imaginecup.util.collision;
using Microsoft.Xna.Framework.Input;

namespace Imaginecup.gameState.option
{
    public class SoundController:ISourceInfoClass
    {
        enum SoundControllerState { None, Press };
        private ISprite m_SoundSprite;

        private EntityInfo m_Info;
        private SoundControllerState m_State;
        
        private int m_MaxX = 575;
        private int m_InitX;

        private ISoundVolume m_VolumeMan;
        
        public SoundController(Vector2 pos , ISoundVolume volumeMan)
        {
            m_Info = new EntityInfo();
            m_Info.mPosition = pos;

            m_State = SoundControllerState.None;
            m_InitX = (int)pos.X;

            m_VolumeMan = volumeMan;

            //  double SoundValue = (m_Info.mPosition.X - m_InitX) / (m_MaxX - m_InitX) ;
            // m_Info.X = soundValue * (m_Max-m_initX) + m_InitX
            m_Info.mPosition.X = m_VolumeMan.GetSound() * (m_MaxX - m_InitX) + m_InitX;
        }

        public void SetSprite(ISprite sprite)
        {
            m_SoundSprite = sprite;
        }

        public void SetSize( Vector2 size)
        {
            m_Info.mSize = size;
        }

        public EntityInfo GetSourceInfo()
        {
            if( m_State == SoundControllerState.None )
            {
                return new EntityInfo(Vector2.Zero, m_Info.mSize);
            }
            return new EntityInfo(new Vector2(m_Info.mSize.X, 0), m_Info.mSize);
        }

        public void Draw(SpriteBatch sb)
        {
            m_SoundSprite.Draw(sb, m_Info);
        }

        public void Release()
        {
            m_SoundSprite.Release();
            m_SoundSprite = null;
        }

        public void Update(GameTime gameTime)
        {
            IMouseInputHandler input = MouseInputHandler.GetInputHandler();
            if( m_State == SoundControllerState.None )
            {
                if (input.IsOneClick() && Collision.CollisionByPoint(m_Info, input.Position))
                {
                    m_State = SoundControllerState.Press;
                }
            }

            else if( m_State == SoundControllerState.Press )
            {

                if(input.MouseState.LeftButton == ButtonState.Released )
                {
                    m_State = SoundControllerState.None;
                }

                else
                {
                    m_Info.SetCenterX( input.Position.X );

                    if( m_Info.mPosition.X < m_InitX )
                    {
                        m_Info.mPosition.X = m_InitX;
                    }
                    else if( m_Info.mPosition.X > m_MaxX )
                    {
                        m_Info.mPosition.X = m_MaxX;
                    }

                    double SoundValue = (m_Info.mPosition.X - m_InitX) / (m_MaxX - m_InitX) ;
                    m_VolumeMan.SetSound(SoundValue);
                }

                
            }
            
        }
    }
}
